package ezXpns.GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import net.miginfocom.swing.MigLayout;

//@author A0086894H
public class LoginGUI extends JDialog {
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			tryLoadLookAndFeel();
			LoginGUI dialog = new LoginGUI();
			dialog.setLocationRelativeTo(null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	JPanel userPanel;
	JScrollPane user_scroll_pane;

	/**
	 * Create the dialog.
	 * 
	 * @throws IOException
	 */
	public LoginGUI() throws IOException {
		setTitle("EzXpns(V0.2) : Select Profile");
		setBounds(100, 100, 550, 361);
		getContentPane().setLayout(new BorderLayout());

		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[14px,grow]", "[14px,grow]"));
		{
			userPanel = new JPanel();
			userPanel.setLayout(new MigLayout("", "[467.00px,grow,fill]", "[]"));
			user_scroll_pane = new JScrollPane(userPanel);
			contentPanel.add(user_scroll_pane, "cell 0 0,aligny top,grow");
		}

		reloadUsers();

		// button panel
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			// button: new user creation
			{
				JButton okButton = new JButton("New User");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						AccountCreationGUI newUser = new AccountCreationGUI();
						newUser.showDialog();
						try {
							reloadUsers();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
				});
				okButton.setActionCommand("New User");
				buttonPane.add(okButton);
			}
			// button: close
			{
				JButton closeButton = new JButton("Close");
				closeButton.setActionCommand("Cancel");
				buttonPane.add(closeButton);
				closeButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
			}
		}
	}

	private void reloadUsers() throws IOException {
		System.out.println("HEY");
		userPanel.removeAll();

		File profileFile = new File("tmp//profiles.txt");
		BufferedReader bufRdr;
		try {
			bufRdr = new BufferedReader(new FileReader(profileFile));

			String line = null;

			while ((line = bufRdr.readLine()) != null) {

				String[] temp = line.split(",");
				ProfilePanel user = new ProfilePanel(temp[0], temp[2]);

				user.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						System.out.println("Run profile");
						GUI gui = new GUI();
						try {
							gui.run(((ProfilePanel) e.getComponent()).getName());
							dispose();
						} catch (IOException e1) {
							e1.printStackTrace();
						}

					}
				});
				userPanel.add(user, "wrap");
			}

			validate();

			bufRdr.close();
		} catch (FileNotFoundException e2) {
			// e2.printStackTrace();
		}
	}

	private static void tryLoadLookAndFeel() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			System.err.println("Couldn't find class for specified look and feel: Nimbus");
		}
	}

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
}